﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BUS;
using DTO;
using System.Data.SqlClient;
namespace ThuVien
{
    public partial class frmNhanVien : DevComponents.DotNetBar.Office2007RibbonForm 
    {

        public frmNhanVien()
        {
            InitializeComponent();
        }

        NhanVienBUS NV_BUS = new NhanVienBUS();
        NhanVienDTO NV_DTO = new NhanVienDTO();
        public DataTable HienDS_BoPhan()
        {
            SqlConnection con = NV_BUS.ChuoiKN_Load();
            con.Open();
            string sql = "select *from bophan";
            SqlCommand cmd = new SqlCommand(sql,con);
            cmd.ExecuteNonQuery();
            DataTable dt = new DataTable();
            SqlDataAdapter da = new SqlDataAdapter(sql, con);
            da.Fill(dt);
            return dt;

         }
        public DataTable Hien_chucVu()
        {
            SqlConnection con = NV_BUS.ChuoiKN_Load();
            con.Open();
            string sql = "select* from chucvu";
            SqlCommand cmd = new SqlCommand(sql, con);
            cmd.ExecuteNonQuery();
            DataTable dt = new DataTable();
            SqlDataAdapter da = new SqlDataAdapter(sql,con);
            da.Fill(dt);
            return dt;
        
        }
        private void frmNhanVien_Load(object sender, EventArgs e)
        {
            
            
            txtMaNV.Text = NV_BUS.Phatsinh_MaNV();
            cbxChucVu.DataSource = Hien_chucVu();
            cbxChucVu.ValueMember = "tenchucvu";
            cbxChucVu.DisplayMember = "tenchucvu";
            cbxBoPhan.DataSource = HienDS_BoPhan();
            cbxBoPhan.ValueMember = "TenBoPhan";
            cbxBoPhan.DisplayMember = "TenBoPhan";
            cbxBoPhan.Text = "Thủ Thư";
            NV_BUS.khoitao_KN();
            NV_BUS.khoitao_KN();
            DataSet dataset = new DataSet();
            dataset = NV_BUS.Hien_DSNV();
            dataGridViewX1.ForeColor = Color.Blue;
            dataGridViewX1.DataSource = dataset.Tables[0];
            
            //txtMaNV.Text = NV_BUS.Phatsinh_MaNV();
          
        }

        private void labelX5_Click(object sender, EventArgs e)
        {

        }

        private void textBoxX5_TextChanged(object sender, EventArgs e)
        {
            
        }

        private void buttonX5_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Bạn có muốn thoát khỏi chương trình??", "THOÁT", MessageBoxButtons.YesNo , MessageBoxIcon.Exclamation) == DialogResult.Yes )
            {
                this.Close();
            }
        }

        private void dataGridViewX1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void cbxBangCap_SelectedIndexChanged(object sender, EventArgs e)
        {
            
        }

        private void txtMaNV_TextChanged(object sender, EventArgs e)
        {
            txtMaNV.BackColor = Color.Blue ;
            txtMaNV.ForeColor = Color.White ;
        }

        private void buttonX1_Click(object sender, EventArgs e)
        {

        }
        private void btnThem_NV_Click(object sender, EventArgs e)
        {
            NV_BUS.khoitao_KN();
            txtMaNV.Text = NV_BUS.Phatsinh_MaNV();
            NV_DTO.Manv = txtMaNV.Text;
            NV_DTO.Tennv = txtHoten.Text;
            NV_DTO.Ngaysinh = NgaySinh.Value;
            NV_DTO.Diachi = txtDiachi.Text;
            NV_DTO.Dienthoai = txtDienThoai.Text;
            if (cbxBangCap.SelectedIndex == 0)
            {
                NV_DTO.Bangcap = comboItem1.Text;
            }
            if (cbxBangCap.SelectedIndex == 1)
            {
                NV_DTO.Bangcap = comboItem2.Text;

            }
            if(cbxBangCap.SelectedIndex==2)
            {
                NV_DTO.Bangcap = comboItem3.Text;
            }
            string temp1;
            if (cbxBoPhan.SelectedIndex==0)
            {
                temp1 = "TK";
            }
            else
            {
                if (cbxBoPhan.SelectedIndex==1)
                {
                    temp1 = "TQ";
                }
                else
                    temp1 = "TT";

            }
            NV_DTO.Mabophan = temp1;
            string temp2;
            if (cbxChucVu.SelectedIndex ==0)
            {
                temp2 = "GĐ";
            }
            else
            {
                if (cbxChucVu.SelectedIndex==1)
                {
                    temp2 = "NV";
                }
                if (cbxChucVu.SelectedIndex==2)
                {
                    temp2 = "PP";
                }
                else
                    temp2 = "TP";
            }
            NV_DTO.Machucvu = temp2;
            try
            {
                NV_BUS.them_nhanVien(NV_DTO);
            }
            catch
            {
                MessageBox.Show("ban nhap khong dung thong tin", "thong bao");
            }
            
            frmNhanVien_Load(sender, e);
        }

        private void cbxChucVu_SelectedIndexChanged(object sender, EventArgs e)
        {
            
            cbxChucVu.DisplayMember = cbxChucVu.SelectedValue.ToString();
            //cbxBoPhan.DisplayMember = cbxBoPhan.SelectedValue.ToString();
            
        }

        private void buttonX2_Click(object sender, EventArgs e)
        {
            NV_DTO.Manv = txtMaNV.Text;
            NV_DTO.Tennv = txtHoten.Text;
            NV_DTO.Ngaysinh = NgaySinh.Value;
            NV_DTO.Diachi = txtDiachi.Text;
            NV_DTO.Dienthoai = txtDienThoai.Text;
            string temp1;
            if (cbxBoPhan.SelectedIndex == 0)
            {
                temp1 = "TK";
            }
            else
            {
                if (cbxBoPhan.SelectedIndex == 1)
                {
                    temp1 = "TQ";
                }
                else
                    temp1 = "TT";

            }
            NV_DTO.Mabophan = temp1;
            string temp2;
            if (cbxChucVu.SelectedIndex == 0)
            {
                temp2 = "GĐ";
            }
            else
            {
                if (cbxChucVu.SelectedIndex == 1)
                {
                    temp2 = "NV";
                }
                if (cbxChucVu.SelectedIndex == 2)
                {
                    temp2 = "PP";
                }
                else
                    temp2 = "TP";
            }
            NV_DTO.Machucvu = temp2;
            if (cbxBangCap.SelectedIndex == 0)
            {
                NV_DTO.Bangcap = comboItem1.Text;
            }
            if (cbxBangCap.SelectedIndex == 1)
            {
                NV_DTO.Bangcap = comboItem2.Text;

            }
            NV_BUS.xoa_NhanVien(NV_DTO);
            frmNhanVien_Load(sender, e);
        }
     //   int dong;

        private void txtHoten_TextChanged(object sender, EventArgs e)
        {
            
        }

        private void NgaySinh_Click(object sender, EventArgs e)
        {
            NgaySinh.BackColor = Color.Red;
            NgaySinh.ForeColor = Color.YellowGreen;
        }

        private void txtDiachi_TextChanged(object sender, EventArgs e)
        {
            
     }

        private void cbxBoPhan_SelectedIndexChanged(object sender, EventArgs e)
        {
           
        }
        private void dataGridViewX1_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
           
        }
        int dong;
        private void dataGridViewX1_RowEnter_1(object sender, DataGridViewCellEventArgs e)
        {
            dong = e.RowIndex;
            try
            {
                txtMaNV.Text = dataGridViewX1.Rows[dong].Cells[0].Value.ToString();
                txtHoten.Text = dataGridViewX1.Rows[dong].Cells[1].Value.ToString();
                NgaySinh.Value = (DateTime)dataGridViewX1.Rows[dong].Cells[2].Value;
                txtDiachi.Text = dataGridViewX1.Rows[dong].Cells[3].Value.ToString();
                txtDienThoai.Text = dataGridViewX1.Rows[dong].Cells[4].Value.ToString();
                cbxBangCap.Text = dataGridViewX1.Rows[dong].Cells[5].Value.ToString();
                cbxChucVu.Text = dataGridViewX1.Rows[dong].Cells[6].Value.ToString();
                cbxBoPhan.Text = dataGridViewX1.Rows[dong].Cells[7].Value.ToString();
            }
            catch {
                txtMaNV.Text = "";
                txtHoten.Text = "";
                NgaySinh.Value = DateTime.Today;
                txtDiachi.Text = "";
                txtDienThoai.Text = "";
                cbxBangCap.Text = "";
                cbxChucVu.Text = "";
                cbxBoPhan.Text = "";
            
            }

        }

        private void buttonX3_Click(object sender, EventArgs e)
        {
            NV_DTO.Manv = txtMaNV.Text;
            NV_DTO.Tennv = txtHoten.Text;
            NV_DTO.Ngaysinh = NgaySinh.Value;
            NV_DTO.Diachi = txtDiachi.Text;
            NV_DTO.Dienthoai = txtDienThoai.Text;
            string temp1;
            if (cbxBoPhan.SelectedIndex == 0)
            {
                temp1 = "TK";
            }
            else
            {
                if (cbxBoPhan.SelectedIndex == 1)
                {
                    temp1 = "TQ";
                }
                else
                    temp1 = "TT";

            }
            NV_DTO.Mabophan = temp1;
            string temp2;
            if (cbxChucVu.SelectedIndex == 0)
            {
                temp2 = "GĐ";
            }
            else
            {
                if (cbxChucVu.SelectedIndex == 1)
                {
                    temp2 = "NV";
                }
                if (cbxChucVu.SelectedIndex == 2)
                {
                    temp2 = "PP";
                }
                else
                    temp2 = "TP";
            }
            NV_DTO.Machucvu = temp2;
            if (cbxBangCap.SelectedIndex == 0)
            {
                NV_DTO.Bangcap = comboItem1.Text;
            }
            if (cbxBangCap.SelectedIndex == 1)
            {
                NV_DTO.Bangcap = comboItem2.Text;
            }
            NV_BUS.Sus_NhanVien(NV_DTO);
            frmNhanVien_Load(sender, e);

        }

        private void buttonX4_Click(object sender, EventArgs e)
        {
            txtMaNV.Text = NV_BUS.Phatsinh_MaNV();
            txtHoten.Text = "";
            NgaySinh.Value = DateTime.Today;
            txtDiachi.Text = "";
            txtDienThoai.Text = "";
            cbxBangCap.Text = "";
            cbxChucVu.Text = "";
            cbxBoPhan.Text = "";
            txtHoten.Focus();
        }

        private void buttonX6_Click(object sender, EventArgs e)
        {
            frmTimKiemNV f = new frmTimKiemNV();
            f.MdiParent = this.MdiParent;
            f.Show();
            this.Close();
        }

    }
}
